package com.loooooo.pm2024.modules.pm.controller;


import com.loooooo.pm2024.data.Result;
import com.loooooo.pm2024.modules.pm.model.WorkLog;
import com.loooooo.pm2024.modules.pm.selector.WorkLogSelector;
import com.loooooo.pm2024.modules.pm.selector.WorkSummarySelector;
import com.loooooo.pm2024.modules.pm.service.WorkLogService;
import com.loooooo.pm2024.modules.pm.service.WorkLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/pm/work/log")
public class WorkLogController {
    @Autowired
    WorkLogService workLogService;

    @RequestMapping("/get")
    String get(@RequestParam(value = "id", required = false) Long id) {
        return "pm/pm.work.log.get";
    }

    @PostMapping(value = "/get", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result get(@RequestParam(value = "id") Long id, @RequestBody(required = false) WorkLogSelector selector) {

        return Result.success(workLogService.get(id, selector)).set("now", System.currentTimeMillis());

    }

    @RequestMapping("/list")
    String listGet() {
        return "pm/pm.work.log.list";
    }

    @PostMapping(value = "/list", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result listPost(@RequestBody(required = false) WorkLogSelector selector) {
        if (selector == null) selector = new WorkLogSelector();

        if (selector.getPageSize() != null) {
            if (selector.getPageNo() == null || selector.getPageNo() < 1) {
                selector.setPageNo(1);
            }
            Result result = new Result();
            int count = workLogService.count(selector);
            result.set("total", count);
            if (count > (selector.getPageSize() * (selector.getPageNo() - 1))) {
                result.setData(workLogService.list(selector));
            }
            return result.set("now", System.currentTimeMillis());
        }
        return Result.success(workLogService.list(selector)).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result update(@RequestBody WorkLog comment) {
        int r = workLogService.update(comment);
        return Result.success(comment).set("affect", r).set("now", System.currentTimeMillis());
    }

    @PostMapping(value = "/summary")
    @ResponseBody
    Result summary(@RequestBody(required = false) WorkSummarySelector selector) {

        return Result.success(workLogService.summaries(selector)).set("now", System.currentTimeMillis());

    }

}
